package com.aiju.common.http.interceptor;


import com.aiju.app.SimpleHttpCallback;
import com.aiju.support.aop.AInterceptor;
import com.aiju.support.aop.AInterceptorHandler;
import com.aiju.widgit.dialog.DialogHelper;

import java.lang.reflect.Method;

/**
 * @author: 佐为
 * @date: 2017-03-21 17:12
 * @version: V1.0
 * @Description: 加载框
 */
public class ProgressDialogInterceptor extends AInterceptor {

    private DialogHelper mDialogHelper;
    private SimpleHttpCallback callback;

    @Override
    protected void addMetodName() {
        methods.add("requestPost");
        methods.add("requestGet");
    }

    @Override
    protected Object interceptor(Object proxied, Method method, Object[] args) {
        return AInterceptorHandler.DEFAULT;//不做拦截
    }

    @Override
    public Object before(Object proxied, Method method, Object[] args) {
        if (mDialogHelper != null) {
            mDialogHelper.showLoadingProgressDialog();
        }
        if (args[2] != null && args[2] instanceof SimpleHttpCallback) {
            callback = (SimpleHttpCallback) args[2];
        }
        return super.before(proxied, method, args);
    }

    @Override
    public Object after(Object proxied, Method method, Object[] args, Object methodReturn) {
        if (callback != null) {
            callback.addResult(new SimpleHttpCallback.IResult() {
                @Override
                public void result(String data) {
                    if (mDialogHelper != null) {
                        mDialogHelper.dismissProgressDialog();
                    }
                }
            });
        } else {
            if (mDialogHelper != null) {
                mDialogHelper.dismissProgressDialog();
            }
        }
        return super.after(proxied, method, args, methodReturn);
    }

    public void setDialogHelper(DialogHelper dialogHelper) {
        this.mDialogHelper = dialogHelper;
    }
}
